﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using OgilvyOne.UKNow.BLL;
using OgilvyOne.UKNow.Model;

public partial class cn_XML_Vote : System.Web.UI.Page
{

     VotesKeyWordsBLL vkwBll = null;
     IList<VotesKeyWordsModel> vkwList = null;
     
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            if (Request.Form["code"]!=null)
            {
                string codestr = Request.Form["code"].ToString();
                if (codestr != null)
                {
                    GetData(Convert.ToInt32(codestr));
                }
            }
            if (Request.Form.Count > 1)
            {
                SetDate();

            }
        }
    }
    private void GetData(int code)
    {
        vkwBll = new VotesKeyWordsBLL(code);
        vkwList = vkwBll.selectKeyWordsNrelsy();
        StringBuilder lastHtml = new StringBuilder();
        foreach (VotesKeyWordsModel vkw in vkwList)
        {
            lastHtml.AppendFormat("{0}|",vkw.Name);
        }
        string lsh = lastHtml.ToString();
        lsh = lsh.Remove(lsh.LastIndexOf('|'), 1);
        if (lsh != "" && lsh != null)
        {
            Response.Write(lsh);
        }
        else
        {
            Response.Write("0");
        }



    }
    private void SetDate()
    {
        string v1 = "";
        string v2 = "";
        string v3 = "";
        int v=Request.Form.Count;
        if (v==5)
        {
            v1 = Request.Form["1"].ToString();
            v2 = Request.Form["2"].ToString();
            v3 = Request.Form["3"].ToString();
        }
        if (v == 4)
        {
            v1 = Request.Form["1"].ToString();
            v2 = Request.Form["2"].ToString();
        }
        if (v ==3)
        {
            v1 = Request.Form["1"].ToString();
        }
        string vx = Request.Form["x"].ToString();
        string[] vs ={ v1,v2,v3};
        VotesKeyWordsModel vkw = new VotesKeyWordsModel();
        int iscode = Convert.ToInt32(Request.Form["code1"]);
        vkwBll = new VotesKeyWordsBLL( iscode);
        vkwList = vkwBll.selectKeyWords();

        foreach (string  v0 in vs)
        {
            vkwList = vkwBll.selectKeyWords(v0);
            vkw = vkwList.Count!=0?vkwList[0]:null;
            if (vkw!=null)
            {
                vkw.Count++;
                vkwBll.Save(vkw
                    );
            }
             
            
        }
        if (vx!=null&&vx!="")
        {
            vkwList = vkwBll.selectKeyWords(vx);
            if (vkwList.Count ==0)
            {
                vkw = new VotesKeyWordsModel();
                vkw.Name = vx;
                vkw.LanguageID = iscode;
                vkw.Type = 0;
                vkwBll.Save(vkw);




            }
        }
        
        

         

    }
}